<?php

namespace app\common\model\Business;

// 引入TP基础模型
use think\Model;

// 引入软删除
use traits\model\SoftDelete;

// 用户收货地址模型
class  Address extends Model
{

    use SoftDelete;

    protected $name = 'business_address';

    protected $deleteTime = 'deletetime';

    protected $append = [
        'region_text', // 省市区
    ];

    public function getRegionTextAttr($value, $data)
    {
        $region = '';

        // 省
        $province = !empty($data['province']) ? $data['province'] : '';

        if (!empty($province))
        {
            $name = model('Region')::where('code', $province)->value('name');
            $region = $name;
        }

        // 市
        $city = !empty($data['city']) ? $data['city'] : '';

        if (!empty($city))
        {
            $name = model('Region')::where('code', $city)->value('name');
            $region .= $name;
        }

        // 区
        $district = !empty($data['district']) ? $data['district'] : '';

        if (!empty($district))
        {
            $name = model('Region')::where('code', $district)->value('name');
            $region .= $name;
        }

        return $region;
    }
}
